﻿#include "uimodelmanagedelegate.h"
#include <QListView>
#include <QDebug>
#include <QPainter>
#include <QVariant>
#include <QPixmap>
#include <QEvent>
#include <QImage>
#include <QMouseEvent>
#include <QPainterPath>
#include <QPushButton>
#include <QRegion>
#include <QPixmap>

#include <shlib/viewmodel/cutils.h>
UiModelManageDelegate::UiModelManageDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
    m_view = dynamic_cast<QListView*>((QWidget*)(parent));
    if(!m_view)
        qDebug() << "view cast failed.";
}

void UiModelManageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSharedPointer<IModel> model = index.data(Qt::UserRole).value<QSharedPointer<IModel>>();

    painter->save();
    painter->setRenderHint(QPainter::Antialiasing);

    if(option.state & QStyle::State_MouseOver)
    {
        painter->setBrush(QColor("#C0C0C0"));
    }
    if(option.state & QStyle::State_Selected)
    {
        painter->setBrush(QColor("#C0C0C0"));
    }

    /*****************************************绘制外边框********************************************/
    QColor lightBlue(173, 216, 230);
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(lightBlue); // 设置画笔的填充颜色
    painter->drawRoundedRect(option.rect, 10, 10);
    painter->restore();

    /*****************************************绘制封面图标********************************************/
    QRect rect  = option.rect;
    QRegion clipRegion(rect, QRegion::Ellipse);
    clipRegion = clipRegion.subtracted(QRegion(rect.adjusted(10, 10, -10, 0), QRegion::Ellipse));
    painter->setClipRegion(clipRegion);

    QPixmap image(":/model_model.png");
    image = image.scaledToWidth(125, Qt::SmoothTransformation);

    // 绘制图像
    painter->drawPixmap(rect.x()+10, rect.y()+10, image);

    // 绘制剩余部分
    painter->setClipRegion(QRegion(rect));
    painter->drawPixmap(rect.x()+10, rect.y()+10, image);

    /*****************************************绘制按钮并保存按钮区域***********************************/

    if(option.state & QStyle::State_MouseOver) {
        painter->save();
        painter->setPen(QPen(Qt::green, 3));
        painter->drawLine(rect.x() + 10, rect.y()+10, rect.x() + 30, rect.y()+10);
        painter->drawLine(rect.x() + 10, rect.y()+20, rect.x() + 30, rect.y()+20);
        painter->drawLine(rect.x() + 10, rect.y()+30, rect.x() + 30, rect.y()+30);
        painter->restore();
    }


    /**********************************绘制模型名称***************************************************/
    QFontMetrics fm(painter->font());
    painter->setFont(QFont("Arial",14));
    auto nameX = rect.x()+15;
    auto nameY = rect.y()+m_itemWidth+15;
    QString sceneName = FontAdjustWidth(QFont("Arial",15),170,index.data(Qt::DisplayRole).toString());
    painter->drawText(QPoint(nameX,nameY),"模型:"+sceneName);

    painter->restore();
}

bool UiModelManageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    return QStyledItemDelegate::editorEvent(event,model,option,index);
}

QSize UiModelManageDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize(m_itemWidth,m_itemHeight);
}

QWidget *UiModelManageDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSharedPointer<IModel> model = index.data(Qt::UserRole).value<QSharedPointer<IModel>>();
    quint16 id = 1;
    QString name = "test";
    UiSceneMenuBtn* btn = new UiSceneMenuBtn(id,name, parent);
    if(!btn)
        return nullptr;
    btn->setText("...");
    btn->setFixedSize(m_menuSize);
    btn->setStyleSheet("background:gray;");
    btn->setFocusPolicy(Qt::NoFocus);
    return btn;
}

void UiModelManageDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    auto btn = dynamic_cast<UiSceneMenuBtn*>(editor);
    if(!btn)
        return;
    btn->setGeometry(getMenuRect(option));
}

QRect UiModelManageDelegate::getMenuRect(const QStyleOptionViewItem &option) const
{
    auto menuX = option.rect.x() + option.rect.width() - m_hSpacing - m_hSpacing;
    auto menuY = option.rect.y() + m_vSpacing + m_vSpacing - m_menuSize.height();
    return QRect(menuX,menuY,m_menuSize.width(),m_menuSize.height());
}

